package components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import org.dom4j.Element;

public class AddLinguisticVariableDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JLabel lblLingHasBeen;
	private ArrayList<LinguisticVariable> variables;

	private JTextField lingNameField;
	private JComboBox lingVNameCombobox;

	/**
	 * Create the dialog.
	 */
	public AddLinguisticVariableDialog(
			final ArrayList<LinguisticVariable> variables) {
		setBounds(100, 100, 450, 300); //sets the location and size of the dialog on screen in case of null layout
		getContentPane().setLayout(new BorderLayout()); // sets the layout to borderLayout
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); //adding borders 
		getContentPane().add(contentPanel, BorderLayout.CENTER); //add the contentpanel to center of screen that is frame
		contentPanel.setLayout(null); // setting layout to null

		this.variables = variables; 
		{
			JLabel lblX = new JLabel("Name:");// labeling the variable textfield in the dialog box
			lblX.setBounds(94, 46, 119, 21); // setting location and size in case of null layout
			contentPanel.add(lblX); // adding to jDialog's body
		}
		lingNameField = new JTextField(); // variable name textfield
		lingNameField.setBounds(223, 40, 109, 27); // sets location and size of text field in screen with null layout
		contentPanel.add(lingNameField); // adding to JDialog's body

		{
			lblLingHasBeen = new JLabel("Linguistic variable has been added"); // label representing the successfull
			// addition of variable
			lblLingHasBeen.setVisible(false); // do not show it on screen for now
			lblLingHasBeen.setBounds(94, 204, 216, 15); // sets the location and size of lblHasBeen on screen
			contentPanel.add(lblLingHasBeen); // adding to JDialog's body
		}
		{
			JPanel buttonPane = new JPanel(); // this panel holds the add and clear buttons
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); // setting flowlayout on button pane
			getContentPane().add(buttonPane, BorderLayout.SOUTH); //add buttonpane to jdialog's body

			{
				JButton okButton = new JButton("Add"); // create add button
				okButton.addActionListener(new ActionListener() { //add actionListener to be called whenever button is clicked
					boolean ifexists; // if the current variable already exists

					public void actionPerformed(ActionEvent e) {
						for (LinguisticVariable lgv : variables) { // loop through all variables currently in list
							if (lingNameField.getText() != null && !lingNameField.getText().equals("") 
									// check of user has entered correct variable name to add
									&& lgv.getName().equalsIgnoreCase(
											lingNameField.getText())) {
								// check if the fields prompted were correctly filled by the user
								ifexists = true; // if already contained in the list
								break; // stop looping

							}

						}
						if (!ifexists) { // if the variable is not already in the list 
							// this ensures that the variable was not already in the list
							variables.add(new LinguisticVariable(
									lingNameField.getText()));
							lblLingHasBeen
									.setText("Linguistic Variable Has been added");
							lblLingHasBeen.setVisible(true); // show that variable successfully added
							JOptionPane.showMessageDialog(null, lblLingHasBeen.getText());
							setVisible(false); // to make dialog box disappear

						}

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton); //adding button to buttonpane
				getRootPane().setDefaultButton(okButton); // setting this button as default in buttonPane
			}
			{
				JButton cancelButton = new JButton("Cancel"); //create cancel button
				cancelButton.addActionListener(new ActionListener() { // add event to cancel button
					public void actionPerformed(ActionEvent e) {
						// clear the variable name field whenever the cancel button is clicked
						lblLingHasBeen.setVisible(false);
						 lingNameField.setText("");
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton); // adding cancel button to button pane in dialog to show up on screen
			}
		}
	}

	// for deleting the linguistic variable
	public AddLinguisticVariableDialog(
			final ArrayList<LinguisticVariable> variables, boolean isDeleting) {
		// this constructor is used to create a delete variable Dialog
		setBounds(100, 100, 450, 300);//sets the location and size of the dialog on screen in case of null layout
		getContentPane().setLayout(new BorderLayout()); // setting border layout to main screen of dialog box
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); // adding border to dialog box which is empty
		getContentPane().add(contentPanel, BorderLayout.CENTER); // align the contents of contentpanel to Center of dialog box
		contentPanel.setLayout(null); // setting null layout in order to add textfields, buttons more easily

		this.variables = variables; // initializing  variables label
		{
			JLabel lblX = new JLabel("Name:");
			lblX.setBounds(94, 46, 119, 21);
			contentPanel.add(lblX); //adding it to dialog screen
		}
		lingVNameCombobox = new JComboBox(); //  create linguistic variables combobox
		lingVNameCombobox.setBounds(223, 40, 109, 27); // set its location and size on dialog box screen
		for (int index = 0; index < variables.size(); index++) { // add all variables currently in the list to combobox
			lingVNameCombobox.addItem(variables.get(index).getName());
		}
		lingVNameCombobox.setSelectedIndex(-1); // set index that no one of variables added is selected when  combobox shows up on screen
		contentPanel.add(lingVNameCombobox); // add combobox to contentPanel to be shown on screen

		{
			lblLingHasBeen = new JLabel("Linguistic variable has been added");// success label shown when linguistic variable is added to the list
			lblLingHasBeen.setVisible(false); // do not show it on screen for now
			lblLingHasBeen.setBounds(94, 204, 216, 15); // set its location and size on screen
			contentPanel.add(lblLingHasBeen); // add it to dialog box
		}
		{
			JPanel buttonPane = new JPanel();//panel holding buttons cancel and Delete
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); // setting layout to align buttons inside it
			getContentPane().add(buttonPane, BorderLayout.SOUTH);// add panel to main jdialog screen

			{
				JButton okButton = new JButton("Delete"); // create delete button
				okButton.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent e) {
						for (int indexv = 0; indexv < variables.size(); indexv++) {
							if (lingVNameCombobox.getSelectedIndex() > -1
									&& variables
											.get(indexv)
											.getName()
											.equalsIgnoreCase(
													lingVNameCombobox
															.getSelectedItem()
															.toString())) {

								// check if the fields prompted were correctly filled by the user and info is found in the 
								//variables
								variables.remove(indexv); // remove the desired choice of the variable
								lblLingHasBeen
										.setText("Linguistic Variable Has been deleted");
								lblLingHasBeen.setVisible(true);
								JOptionPane.showMessageDialog(null, "Variable Deleted..");
								setVisible(false); // To make Dialog Disappear
								break;

							}

						}

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton); // add buttons to buttonpane inside jdialog
				getRootPane().setDefaultButton(okButton); // set this button as default
			}
			{
				JButton cancelButton = new JButton("Cancel"); // creates the cancel button and adds the event listeners
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						lblLingHasBeen.setVisible(false);
						lingNameField.setText(""); // clear the contents of textfield when the cancel button is clicked
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton); // add this to main panel
			}
		}
	}
}